﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LockTime
{
    /// <summary>
    /// 字符串按钮
    /// 可以设置背景色，鼠标悬浮色，鼠标点击色
    /// 字符串的显示坐标
    /// </summary>
    public class StringButton:Button
    {
        Color clr1;
        //颜色
        private Color color = Color.Teal;
        //悬浮色
        private Color hovercolor = Color.FromArgb(0, 0, 140);
        //点击色
        private Color clickcolor = Color.FromArgb(160, 180, 200);
        //文字坐标
        private int textX = 0;
        private int textY = 0;
        //显示的文字
        private string text = "";

        //Invalidate()会触发重绘的OnPaint事件
        //背景色
        public Color BZBackColor
        {
            get { return color; }
            set { color = value; Invalidate(); }
        }
        //悬浮色
        public Color MouseHoverColor
        {
            get { return hovercolor; }
            set { hovercolor = value;Invalidate(); }
        }
        //点击色
        public Color MouseClickColor
        {
            get { return clickcolor; }
            set { clickcolor = value;Invalidate(); }
        }
        //文本坐标
        public int TextLocation_X
        {
            get { return textX; }
            set { textX = value; Invalidate(); }
        }
        public int TextLocation_Y
        {
            get { return textY; }
            set { textY = value; Invalidate(); }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public StringButton()
        {
            //一些基本设定
            this.Size = new Size(180, 36);
            this.ForeColor = Color.White;
            this.FlatStyle = FlatStyle.Flat;
            this.Font = new Font("Microsoft YaHei UI", 18.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(0)));
            text = this.Text;
        }
        /// <summary>
        /// 重写鼠标进入事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            //改变悬浮色，保存原色
            clr1 = color;
            color = hovercolor;
        }
        /// <summary>
        /// 重写鼠标离开事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            //恢复原色
            color = clr1;
        }
        /// <summary>
        /// 重写鼠标按下事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            //改变点击色
            color = clickcolor;
        }
        /// <summary>
        /// 重写鼠标抬起事件
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            //恢复原色
            color = clr1;
        }
        /// <summary>
        /// 重写绘制事件
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            text = this.Text;
            //设定文字放置的坐标
            Point p = new Point(textX, textY);
            //画出按钮方框
            pevent.Graphics.FillRectangle(new SolidBrush(color), ClientRectangle);
            //画出文字
            pevent.Graphics.DrawString(text, this.Font, new SolidBrush(this.ForeColor), p);
        }
    }
}
